import requests,json
import pandas as pd
key='132ff7ab6a24be39c427ea69fea78f20'
# 地理编码

def geocode(key,address,city=None,batch='false',sig=None,output='json',callback=None):
    """"获取好的API的地理编码信息"""
    url='https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
    'key':key,
    'address':address,
    'city':city,
    'batch':batch,
    'sig':sig,
    'output':output,
    'callback':callback
    }
    response=requests.get(url,params=params)
    results=response.json()['geocodes'][0]['location']
    # return
    return results


# 逆地理编码

def regeo(key,location,poitype=None,radius='1000',extensions='base',batch='false',roadlevel=None,sig=None,
          output='json',callback=None,homeorcorp='0'):
    url='https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key':tang_key,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'sig':sig,
        'output':output,
        'callback':callback,
        'homeorcorp':homeorcorp
    }
    response=requests.get(url,params)
    results=response.json()
    return results

# 路径规划

def geocode(key,address,city=None,batch=None)->str:
    """获取高德API的地理编码
    注释：key是指高德API的秘钥，请先去注册
          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":tang_key,
        "address":address,
        "city":city,
        "batch":batch
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    # 返回值
    return results

# 步行路径规划

def walking(key,origin,location,sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# 公交路径规划

def transit(origin, location, city, cityid=None, extensions='base', strategy=0, nightflag=0,
            date=None, time=None, sig=None, output='json', callback=None):
    # 公交路径规划
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params = {
        'key': key,
        'origin': origin,
        'destination': location,
        'city': city,
        'cityid': cityid,
        'extensions': extensions,
        'strategy': strategy,
        'nightflag': nightflag,
        'date': date,
        'time': time,
        'sig': sig,
        'output': output,
        'callback': callback

    }
    response = requests.get(url, params=params)
    results = response.json()
    # return
    return results

# 驾车路径规划

def driving(key,origin,destination,extensions):
    url='https://restapi.amap.com/v3/direction/driving?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'extensions':extensions,
        "strategy":strategy
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# 骑行路径规划

def bicycling(key,origin,destination):
    url='https://restapi.amap.com/v4/direction/bicycling?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination
    }
    response=requests.get(url,params)
    results=response.json()
    return results

# 货车路径规划

def truck(key,origin,location,size):
    url='https://restapi.amap.com/v4/direction/truck?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'size':size
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results

# 距离测量

def distance(origins, location, type=''):
    url = 'https://restapi.amap.com/v3/distance?parameters'
    key = '18ef7dd6aeefc21dade574262c00f213'
    params = {
        'key': key,
        'origins': origins,
        'destination': location,
        'type': type

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


# 行政区域查询

def district(key,keywords=None,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output
    }
    response=requests.get(url,params)
    results=response.json()
    return results

# 关键字搜索

def text(keywords,city='',extensions='',citylimit=''):
    url='https://restapi.amap.com/v3/place/text?parameters'
    key='132ff7ab6a24be39c427ea69fea78f20'
    params={
        'key':key,
        'keywords':keywords,
        'city':city,
        'extensions':extensions,
        'citylimit':citylimit
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# 周边搜索

def around(location, keywords=''):
    url = 'https://restapi.amap.com/v3/place/around?parameters '
    key = '132ff7ab6a24be39c427ea69fea78f20'
    params = {
        'key': key,
        'keywords': keywords,
        'location': location

    }
    response = requests.get(url, params)
    results = response.json()
    return results


# 多边形搜索

def polygon(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':key,
        'polygon':polygon,
        'keywords':keywords,
        'types':types,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# ID查询

ddef detail(id):
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    key = '132ff7ab6a24be39c427ea69fea78f20'
    params={
        'key':key,
        'id':id,
    }
    response=requests.get(url,params)
    results=response.json()

    return results

# IP定位

def ip(key, id):
    url = 'https://restapi.amap.com/v3/ip?parameters'
    key = '132ff7ab6a24be39c427ea69fea78f20'
    params = {
        'key': key,
        'id': id

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


# 静态地图

def amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic='0',sig=None):
    url='https://restapi.amap.com/v3/staticmap?parameters'
    params={
    'key':key,
    'location':location,
    'zoom':zoom,
    'scale':scale,
    'markers':markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig
    }
    r=requests.get(url,params)
    data=r.url
    return data

# 坐标转换

def convert(key,locations,coordays='',sig='',output=''):
    url='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':key,
        "locations":locations,
        'coordays':coordays,
        'sig':sig,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# 天气查询

def weather(key,city,extensions='base',output=''):
    url='https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':key,
       'city':city,
        'extensions':extensions,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r

# 输入提示

def input(key,keywords,type=None,location=None,city=None,
          citylimit='false',datatype=None,sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    r=response.json()
    return r

# 交通态势

def traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/traffic/status/rectangle?parameters'
    params={
        'key':key,
        'rectangle':rectangle,
        'level':level,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    r=response.json()
    return r